import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreFetchService extends BaseService<any> {
  private readonly logger = new Logger(CoreFetchService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * image
   * 对应 PHP: CoreFetchService_core::image()
   * 逻辑类型: undefined - undefined
   */
  async image(url: number, site_id: number, file_dir: number, storage_type: number) {
    // 基于PHP真实逻辑: image
    // PHP原文: if(empty($url)) throw new UploadFileException('OSS_FILE_URL_NOT_EXIST');         $this->upload_driver = $this->driver($site_id, $storage_type);       ...
if(!url)) throw new UploadFileBusinessException('OSS_FILE_URL_NOT_EXIST'];
        this.upload_driver = this.driver(site_id, storage_type];
        [ext, link) = explode(' + ', strrev(url)];
        const ext = !ext) ? strrev(ext) : 'jpg';
        const file_path = this.upload_driver.createFileName(link, ext];

        const dir = this.root_path  + '/'.  file_dir + '/'.file_path;
        const result = this.upload_driver.fetch(url, dir);

        //读取上传附件的信息用于后续得校验和数据写入
        if (result) {
            return [
                url: this.upload_driver.getUrl(dir)
            ];
  }
}
